package com.daxt.common.redis;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.daxt.base.common.util.RedisUtil;
import com.daxt.mapper.sys.SystemConfigMapper;
import com.daxt.model.service.base.vo.SystemConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;


/**
 *
 * @Description redis 缓存类
 * @DOC 次启动类会在启动的时候加载 sysconfig表的配置信息
 *      并且同时同步信息到redis 缓存
 * @Date 2021-08-27
 * @Created by lig
 */
@Component
public class RedisCache {

    private final static Logger log = LoggerFactory.getLogger(RedisCache.class);

    private static RedisTemplate<String, Object> redisTemplate;

    @Autowired
    private SystemConfigMapper systemConfigMapper;

    @Value("${spring.profiles.active}")
    private String profilesActive;


    /**
     *
     * @param redisTemplate
     */
    @Autowired
    public void setRedisTemplate(RedisTemplate<String, Object> redisTemplate) {
        if (null == redisTemplate) {
            log.info("Redis初始化配置失败，请检查配置项");
        } else {
            log.info("开始加载缓存。。。。。！");
            QueryWrapper<SystemConfig> queryWrapper=new QueryWrapper<SystemConfig>();
            List<SystemConfig> configsList = systemConfigMapper.selectList(queryWrapper);
            for (SystemConfig config : configsList)
            {
                RedisUtil.setSysConfig(config.getSysKey(),config.getSysValue());
            }

            //启动配置参数  例：dev  test等
            RedisUtil.setSysConfig("PROFILES_ACTIVE",profilesActive);

            log.info("加载缓存成功。。。。。！");
        }
    }

}
